package com.cat.dataStructure02;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-product-of-three-elements-after-one-replacement/
 * @create 2025/11/2 18:41
 * @since JDK17
 */

public class Solution99 {
    public long maxProduct(int[] nums) {
        Arrays.sort(nums);
        // 3种情况
        int n = nums.length, min = ((int) -1e5), max = ((int) 1e5);
        long p1 = (long) nums[0] * nums[n - 1], p2 = (long) nums[0] * nums[1], p3 = (long) nums[n - 1] * nums[n - 2];
        long ans = Math.max(p1 * min, p1 * max);
        ans = Math.max(ans, Math.max(p2 * max, p2 * min));
        ans = Math.max(ans, Math.max(p3 * max, p3 * min));
        return ans;
    }
}
